//
//  BudgetTool.m
//  预购清单
//
//  Created by liujianlong on 2022/10/17.
//

#import "BudgetTool.h"

@implementation BudgetTool
+ (void)find:(NSString*)account stamp:(NSInteger)stamp complete:(nullable void(^) (Budget *budget))block{
    NSDateComponents *date = [NSDate dateOfStamp:stamp];
    
    NSString* where = [NSString stringWithFormat:@"where %@=%@ and %@=%@ and %@=%@",bg_sqlKey(@"account"),bg_sqlValue(account),bg_sqlKey(@"year"),bg_sqlValue(@(date.year)),bg_sqlKey(@"month"),bg_sqlValue(@(date.month))];
    NSInteger cout = [Budget bg_count:nil where:where];
    if (cout <= 0) {
        if (block) {
            Budget *model = [Budget new];
            model.account = account;
            model.stamp = stamp;
            model.year = [NSString stringWithFormat:@"%d",(int)date.year];
            model.month = [NSString stringWithFormat:@"%d",(int)date.month];
            model.total = 0;
            model.payed = 0;
            model.waitPayed = 0;
            return block(model);
        }
    }
    
    [Budget bg_findAsync:nil where:where complete:^(NSArray * _Nullable array) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (block) {
                Budget *model = array[0];
                block(model);
            }
        });
    }];
}
+ (void)update:(Budget *)budget complete:(nullable void(^) (BOOL success))block{
    
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        [budget bg_saveOrUpdateAsync:^(BOOL isSuccess) {
            dispatch_async(dispatch_get_main_queue(), ^{
                dispatch_group_leave(group);
            });
        }];
    });
    
    for (Prepay *model in budget.array) {
        dispatch_group_enter(group);
        dispatch_group_async(group, queue, ^{
            [PrepayTool update:model complete:^(BOOL success) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    dispatch_group_leave(group);
                });
            }];
        });
    }
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        if (block) {
            block(YES);
        }
    });
}

+ (void)delete:(Budget*)budget complete:(nullable void(^) (BOOL success))block{
    NSString* where = [NSString stringWithFormat:@"where %@=%@ and %@=%@",bg_sqlKey(@"account"),bg_sqlValue(budget.account),bg_sqlKey(@"stamp"),bg_sqlValue(@(budget.stamp))];
    
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        [Budget bg_deleteAsync:nil where:where complete:^(BOOL isSuccess) {
            dispatch_async(dispatch_get_main_queue(), ^{
                dispatch_group_leave(group);
            });
        }];
    });
    
    for (Prepay *model in budget.array) {
        dispatch_group_enter(group);
        dispatch_group_async(group, queue, ^{
            [PrepayTool delete:model complete:^(BOOL success) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    dispatch_group_leave(group);
                });
            }];
        });
    }
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        if (block) {
            block(YES);
        }
    });
    
}
@end
